package net.jielan188.hd.util;

import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.net.URLConnection;

public class SmsUtil {

  //TODO   短信通道更改
  private static final String MER_ID = "08918";
  private static final String KEY = "n923s6w07t7nfxy1";

  //TODO  这里不用改
  private static final String URL = "http://www.jsmms.com/messageSend.do";

  /**
   * 发送短信
  */
  public static void sendSms( String mobile, String content ) {

    try {
      Map<String, String> param = new HashMap<String, String>();

      param.put( "merId", MER_ID );
      param.put( "mobile", mobile );
      param.put( "content", content );
      param.put( "rank", "" );
      param.put( "reservedTime", "" );
      param.put( "exCode", "123456" );
      param.put( "tranCode", "1001" );
      param.put( "sign", sign( param, KEY ) );
      param.put( "content", URLEncoder.encode( content, "utf-8" ) );
      String reqParams = mapParamsToUrl( param );
      sendPost( URL, reqParams );
    } catch ( Exception e ) {
      e.printStackTrace();
    }
  }

  /**
   * 短信发送网关签名工具
  */
  private static String sign( Map<String, String> param, String secretKey )
			throws NoSuchAlgorithmException, UnsupportedEncodingException {

    StringBuffer signStr = new StringBuffer();
    signStr.append( param.get("merId") )
      .append( param.get("mobile") )
      .append( param.get("content") )
      .append( param.get("rank") )
      .append( param.get("reservedTime") )
      .append( param.get("exCode") )
      .append( param.get( "tranCode" ) ).append( secretKey );
    return DigestUitl.MD5LowerCase( signStr.toString() );
  }

  /**
   * 发送post请求
  */
  public static String sendPost( String url, String param ) {

    PrintWriter out = null;
    BufferedReader in = null;
    StringBuffer result = new StringBuffer();
    try {
      URL realUrl = new URL(url);
      URLConnection conn = realUrl.openConnection();
      conn.setRequestProperty( "accept", "*/*" );
      conn.setRequestProperty( "connection", "Keep-Alive" );
      conn.setRequestProperty( "user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)" );
      conn.setDoOutput(true);
      conn.setDoInput(true);
      out = new PrintWriter( conn.getOutputStream() );
      out.print( param );
      out.flush();
      InputStream is = conn.getInputStream();
      byte[] b = new byte[1024];
      int len = -1;
      while ( ( len = is.read(b) ) != -1 ) {
        result.append( new String( b, 0, len, "utf-8" ) );
      }
      return result.toString();
    } catch ( Exception e ) {
      e.printStackTrace();
    } finally {
      try{
        if ( out != null ) out.close();
        if ( in != null ) in.close();
      } catch ( IOException ex ) {}
    }
    return "";
  }

  /**
   * 将参数Map转换为请求url
  */
  public static String mapParamsToUrl( Map<String, String> params ) {

    StringBuilder sb = new StringBuilder();
    for ( String s : params.keySet() ) {

      sb.append( s ).append( "=" ).append( params.get(s) ).append( "&" );
    }
    if ( params.size() > 1 )
      sb.delete( sb.length() - 1, sb.length() );
    return sb.toString();
  }

}